# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openexr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.1
pkgrel=1
pkgdesc="Openexr library for EXR images (mingw-w64)"
arch=('any')
url="http://www.openexr.com/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"  "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-ilmbase" "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=("https://download.savannah.nongnu.org/releases/openexr/${_realname}-${pkgver}.tar.gz"
        0001-mingw-w64-fix.patch
        0002-openexr-2.1.0-headers.patch
        0003-openexr-2.1.0_aligned-malloc.patch
        0004-openexr-2.1.0_cast.patch
        0005-cmake-win-building.patch
        0006-cmake-soversion.patch
        0007-mingw-dll-install.patch
        0008-change-suffixes-for-64bit-literals.patch
        0009-win-dwalookups.patch
        0010-openexr-2.2.0-mingw-utf8.patch
        0011-fix-crashes-on-windows.patch
        0012-pull93.patch
        0100-mingw-gcc6-bug.patch)
sha256sums=('8f9a5af6131583404261931d9a5c83de0a425cb4b8b25ddab2b169fbf113aecd'
            'becdb62e22589fec7baa90690f4751869fa97f3c4af0cfc022f37ad371d2509f'
            'dd137e83e3b21eb3cd6f8b03922bd576d321c52c66f3b92a3611c1b020d86f1f'
            'ce0aa05382dbd9c76d350ce8f5e56a4f62dfad3f241004cf27e2e6e3ecc77f17'
            'af9f6389d1994de72e9ba2249e81603fb54d3d879feb9d1144328268c9bf4714'
            '67e073af42b3c23600be7de7a8302c673184106f2d69628da1fb0339624f5eb7'
            '7652ba3b80d2c7446dbcb4f13f3d215c966a8ce4ea36b945ea140e19a86a8985'
            '1071314bb59cfb148bce90f5064f9ea2bf7e181e5321bd0129ec5aad6a30c17c'
            'b761df0a62a0e0f54f1d672ac0cc6d4373fd69a55004f3eda0aa72b553f363a7'
            '201d05b2eac8ca74c796012584a800e78aa49919970b50399f37d4be8c83c0fc'
            '9a5c7b74d89bd05122dbe4ed8a03e3d54a7f08fad99f8aa1cf235716b13237db'
            '5d51845f3e37bb0022e464a5231382fae9ab04ffa080e0a90bed1bb86cd6127e'
            '38297ab67d9cc2c6b2a8799106a30e14577db8c7d0d62d1681273f2e4c75ea7b'
            '18bab401e87e1ce95e25e307b413b1fb1fe0132c115bae1df6f61ec6c0cca7d7')

prepare(){
  cd "${srcdir}/${_realname}-$pkgver"

  sed -i 's/#define ZLIB_WINAPI/\/\/#define ZLIB_WINAPI/g' IlmImf/ImfZipCompressor.cpp
  sed -i 's/#define ZLIB_WINAPI/\/\/#define ZLIB_WINAPI/g' IlmImf/ImfPxr24Compressor.cpp

  patch -p1 -i ${srcdir}/0001-mingw-w64-fix.patch
  patch -p1 -i ${srcdir}/0002-openexr-2.1.0-headers.patch
  patch -p1 -i ${srcdir}/0003-openexr-2.1.0_aligned-malloc.patch
  patch -p1 -i ${srcdir}/0004-openexr-2.1.0_cast.patch
  patch -p1 -i ${srcdir}/0005-cmake-win-building.patch
  patch -p1 -i ${srcdir}/0006-cmake-soversion.patch
  patch -p1 -i ${srcdir}/0007-mingw-dll-install.patch
  patch -p1 -i ${srcdir}/0008-change-suffixes-for-64bit-literals.patch
  patch -p1 -i ${srcdir}/0009-win-dwalookups.patch
  patch -p2 -i ${srcdir}/0010-openexr-2.2.0-mingw-utf8.patch
  patch -p2 -i ${srcdir}/0011-fix-crashes-on-windows.patch
  patch -p2 -i ${srcdir}/0012-pull93.patch
  patch -p1 -i ${srcdir}/0100-mingw-gcc6-bug.patch

}

build() {
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/OpenEXR"
  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DNAMESPACE_VERSIONING=ON \
    -DUSE_ZLIB_WINAPI=OFF \
    ../${_realname}-${pkgver}

  #-DCMAKE_VERBOSE_MAKEFILE=ON
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
